<!doctype html>
<html>
    <head>
        <meta charset="UTF-8"/>
        <title>添加品牌</title>
	    <link rel="stylesheet" type="text/css" href="/webapp/css/easyui.css">
	    <link rel="stylesheet" type="text/css" href="/webapp/css/icon.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/color.css">
        <link rel="stylesheet" type="text/css" href="/webapp/css/public.css">
	    <script type="text/javascript" src="/webapp/js/lib/jquery.min.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/jquery.easyui.min.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/jquery.global.settings.js"></script>
	    <script type="text/javascript" src="/webapp/js/lib/ajaxfileupload.js"></script>
	    <script type="text/javascript" src="/webapp/js/common/config.js"></script>
        <script type="text/javascript" src="/webapp/js/lib/lang/zh-cn/Convert_Pinyin.js"></script>
    </head>
    <body>
       <div class="easyui-container-fluid">
          <div class="easyui-panel" style="border: none;">
            <div class="nav" id="nav"></div> 
            <div class="easyui-panel" style="border:none;">
              <form id="ff" method="post">
                <div style="margin-bottom:20px">
                  <label class="item-label"><span class="item-required">*</span>名称：</label>
                  <input class="easyui-textbox name" type="text" name="name" id="name" data-options="required:true">
                  <label class="item-label"><span class="item-required">*</span>拼音：</label>
                  <input class="easyui-textbox py" type="text" name="prettyPinyin" id="prettyPinyin" data-options="required:true">
                    <label class="item-label"><span class="item-required">*</span>所属品类：</label>
                    <select id="categorySelect" class="easyui-combobox" style="width:174px;" name="categoryCodes" data-options="multiple:true" data-options="required:true"></select>

                </div>
                <div style="margin-bottom:20px">
                  <label class="item-label">曾用名：</label>
                  <input class="easyui-textbox " type="text" name="originalName">
                  <label class="item-label"><span class="item-required">*</span>英文名：</label>
                  <input class="easyui-textbox cname" type="text" name="englishName" data-options="required:true">
                  <label class="item-label">曾用英文名：</label>
                  <input class="easyui-textbox" type="text" name="originalEnglishName">
                </div>
                <div style="margin-bottom:20px;">
                  <label class="item-label">别名：</label>
                  <input type="text" name="alias" class="easyui-tagbox bm" id="bmlist" data-options="multiline:true" value="" style="width:440px;min-height:120px;" multiline="true"/>
                </div>
                <div style="margin-bottom:20px;">
                   <label class="item-label">国内品牌：</label>
                  <select name="international" class="easyui-combobox" style="width:128px;" data-options="panelHeight:'auto'">
                    <option value="FOREIGN" >否</option>
                    <option value="DOMESTIC" selected="selected">是</option>
                  </select> 
                  <label class="item-label">互联网：</label>
                  <select name="internet" class="easyui-combobox" style="width:128px;" data-options="panelHeight:'auto'">
                    <option value="false" selected="selected">否</option>
                    <option value="true">是</option>
                  </select> 
                  <label class="item-label">客户品牌：</label>
                    <select name="custom" class="easyui-combobox" style="width:128px;" data-options="panelHeight:'auto'">
                    <option value="false" selected="selected">否</option>
                    <option value="true">是</option>
                  </select> 
                </div>
                <div style="margin-bottom:20px">
                    <label class="item-label">集团：</label>
                    <input class="easyui-textbox" type="text" name="group"/>
                    <label class="item-label">父品牌：</label>
                    <select class="easyui-combobox" id="parentBrandSelect" name="parentBrandCode" style="width:170px;">
                    </select>
                </div>
                <div style="margin-left:100px;margin-botttom:20px;">
                    <a href="javascript:void(0)" class="easyui-linkbutton c8" onclick="submitForm()" style="width:80px">保存</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" onclick="cancel()" style="width:80px">取消</a>
                </div>              
              </form>     
            </div>
          </div> 
       </div>  
    </body>
    <script type="text/javascript">
         var id = getQueryParameter("id");
         $(function(){
             setNav();

             $.when(initCategorySelect("categorySelect"),initBrandSelect("parentBrandSelect")).done(function () {
                 initFormData();
             });
             //自动获取拼音
             $("input", $("#name").next("span")).blur(function () {
                 var name = $('#name').textbox("getValue");
                 if (name) {
                     //获取全写拼音（调用js中方法）
                     var fullName = pinyin.getFullChars(name);
                     //赋值
                     $('#prettyPinyin').textbox("setValue", fullName);
                 }
             });
         });

         function initFormData(){
        	if(id){
                $.ajax({
                  url:config.url +"/brand/"+id,
                  type:'get',
                  xhrFields:{withCredentials:true},
                  success:function(data){
                      $('#name').textbox({
                          editable: false
                      })
                    //填充表单
                      if(data){
                          var brand = changeProperty2String(data);
                          if(!brand.alias){
                              brand.alias = [];
                          }

                          $("#ff").form("load",brand);
                      }   
                  },
                  error:function(){

                  }
                });
              }
         }
         function submitForm(){
             var a =$(".name").textbox('getValue');
             var py =$(".py").textbox('getValue');
             var cname =$(".cname").textbox('getValue');
             var bm = $("#bmlist").combobox('getValues');
             var names=chack_name(a);
             var pys=chack_name(py);
             var cnames=chack_name(cname);
             var bms=chack_name(bm);
             var categoryCodes = $("#categorySelect").combotree("getValues");//.toString()
             if((categoryCodes.length==1 && !categoryCodes[0]) ||categoryCodes.length==0){
                 $.messager.alert("错误","请选操作品类","error");
                 return;
             }
             if(names || pys|| cnames|| bms){
                 $("#ff").form("submit",{
                  onSubmit:function(){
                    var valid = $(this).form('enableValidation').form('validate');
                    if(!valid)return false;
                    var brand = getFormData(document.getElementById("ff"));
                      brand.name=a;
                      brand.prettyPinyin=py;
                      brand.englishName=cname;
                    var method = "POST";
                    if(id){
                      method = "PATCH";
                      brand.id = id;
                    }
                    if(brand['alias'] && typeof brand['alias'] === 'string'){
                    	brand['alias'] = [brand['alias']]
                    }
                      brand.alias=bm;
                      var categoryCodes = $("#categorySelect").combotree("getValues");//.toString()
                      brand.categoryCodes = categoryCodes;
                      // if(brand.categoryCodes.length==1 && !brand.categoryCodes[0]){
                      //     $.messager.alert("错误","请选操作品类","error");
                      //     return;
                      // }
                    $.ajax({
                       url:config.url+"/brand",
                       type:method,
                       data:JSON.stringify(brand),
                       dataType:'json',
                       contentType:'application/json;charset=UTF-8',
                       xhrFields:{withCredentials:true},
                       beforeSend:function(){
                           $.messager.progress({
                               title:"请稍等",
                               interval:50,
                               text:"操作进行中..."
                           });
                       },
                       complete:function(){
                           $.messager.progress("close");
                       },
                       success:function(data){
                           if(data.id){
                               $.messager.alert("提示","操作成功","info",function(){
                                   window.parent.closeCurrentTab(true);
                               });
                           }else{
                               $.messager.alert("提示","品牌重复","error");
                           }

                        },
                        error:function(XMLHttpRequest,textStatus,thrown){
                            $.messager.alert("提示","操作失败","error");
                        }
                    });
                     return false;
                 }
            });
             } else{
                 $("#ff").form("submit",{
                     onSubmit:function(){
                         var valid = $(this).form('enableValidation').form('validate');
                         if(!valid)return false;
                         var brand = getFormData(document.getElementById("ff"));
                         var method = "POST";
                         if(id){
                             method = "PATCH";
                             brand.id = id;
                         }
                         if(brand['alias'] && typeof brand['alias'] === 'string'){
                             brand['alias'] = [brand['alias']]
                         }
                         brand['prettyPinyin'] = $("#prettyPinyin").textbox("getValue");
                         var categoryCodes = $("#categorySelect").combotree("getValues");//.toString()
                         if((categoryCodes.length==1 && !categoryCodes[0]) ||categoryCodes.length==0){
                             $.messager.alert("错误","请选操作品类","error");
                             return;
                         }
                         brand.categoryCodes = categoryCodes;
                         $.ajax({
                             url:config.url+"/brand",
                             type:method,
                             data:JSON.stringify(brand),
                             dataType:'json',
                             contentType:'application/json;charset=UTF-8',
                             xhrFields:{withCredentials:true},
                             beforeSend:function(){
                                 $.messager.progress({
                                     title:"请稍等",
                                     interval:50,
                                     text:"操作进行中..."
                                 });
                             },
                             complete:function(){
                                 $.messager.progress("close");
                             },
                             success:function(data){
                                 if(data.id) {
                                     $.messager.alert("提示", "操作成功", "info", function () {
                                         window.parent.closeCurrentTab(true);
                                     });
                                 }else{
                                     $.messager.alert("提示", "品牌已存在或品类存在错误", "error");
                                 }
                             },
                             error:function(XMLHttpRequest,textStatus,thrown){
                                 $.messager.alert("提示","操作失败","error");
                             }
                         });
                         return false;
                     }
                 });
             }
         }
         function cancel(){
       	    window.parent.closeCurrentTab();
         }
         function chack_name(str){
             var pattern = new RegExp("[`~!@#$^&*()=|{}':;',\\[\\].<>《》/?~！@#￥……&*（）——|{}【】‘；：”“'。，、？ ]");
             if (pattern.test(str)){
                 return true;
             }
             return false;
         }
    </script>
</html>
